package com.luli.controllor;

import com.luli.Utils.RegisterUtils;
import com.luli.constant.Dict;
import com.luli.entity.login.LoginParam;
import com.luli.entity.login.LoginUserVo;
import com.luli.entity.login.RegisterUser;
import com.luli.entity.login.SysUsers;
import com.luli.entity.response.ResponseMsg;
import com.luli.service.LoginService;
import com.luli.service.UsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.luli.Utils.ResponseMsgUtil.getFailMsg;
import static com.luli.Utils.ResponseMsgUtil.getSuccessMsg;
import static com.luli.enumeration.MessageEnum.REGISTER_ERRORS;
import static com.luli.enumeration.MessageEnum.REGISTER_SUCCESS;

/**
 * 陆离
 * 2022/10/18 16:03:13
 */
@RestController
@Slf4j
@RequestMapping("/userInfo")
public class LoginController {

    @Autowired
    private LoginService loginService;


    @Autowired
    private UsersService usersService;

    /**
     * 用户注册
     *
     * @param registerUser
     * @return
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResponseMsg<?> addPhoto(@RequestBody RegisterUser registerUser) {
        ResponseMsg<?> responseMsg = RegisterUtils.checkUser(registerUser);
        try {
            log.info("注册信息,{}",responseMsg.getData());
            RegisterUser userInfo = (RegisterUser)responseMsg.getData();
            Boolean userInfoIs = usersService.insertUser(userInfo);
            if (userInfoIs){
                return getSuccessMsg(REGISTER_SUCCESS.getCode(),REGISTER_SUCCESS.getMsg(),
                        "密码强度:"+userInfo.getPassWordSecurity() + "；申请账号为：" + userInfo.getAccount());
            }
            return getFailMsg(REGISTER_ERRORS.getCode(),REGISTER_ERRORS.getMsg(),userInfo);
        } catch (Exception e) {
            return getFailMsg(responseMsg.getHead().getRequestId(), responseMsg.getHead().getRespDesc(), e.getMessage());
        }
    }

    /**
     * 登录
     *
     * @param loginParam
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResponseMsg<?> login(@RequestBody LoginParam loginParam) {
        try {
            return loginService.login(loginParam);
        } catch (Exception e) {
            return getFailMsg(null, "获取token失败", e.getMessage());
        }
    }

    /**
     * 根据token获取用户信息
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/currentUser", method = RequestMethod.GET)
    public ResponseMsg<?> currentUser(@RequestHeader("Authorization") String token) {
        try {
            ResponseMsg<?> userByToken = loginService.findUserByToken(token);
            SysUsers sysUsers = (SysUsers) userByToken.getData();
            LoginUserVo loginUserVo = new LoginUserVo();
            loginUserVo.setId(sysUsers.getId());
            loginUserVo.setName(sysUsers.getName());
            loginUserVo.setAccount(sysUsers.getAccount());
            loginUserVo.setAddress(sysUsers.getAddress());
            return getSuccessMsg("200", "查询成功", loginUserVo);
        } catch (Exception e) {
            return getFailMsg(null, "获取token失败", e.getMessage());
        }
    }


    /**
     * 注销用户
     * @param account
     * @param verifyDelCode 1:取消删除 0：确认删除
     * @return
     */
    @RequestMapping(value = "/delUser", method = RequestMethod.DELETE)
    public ResponseMsg<?> delUser(String account,String verifyDelCode,String token) {
        try {
            if (Dict.ONE.equals(verifyDelCode)){
                ResponseMsg<?> responseMsg = loginService.delUser(account);
                return responseMsg;
            }else {
                return getFailMsg(null, "取消注销", null);
            }
        } catch (Exception e) {
            return getFailMsg(null, "删除失败", e.getMessage());
        }
    }

}
